package com.biz.primus.ms.member.dao.redis;

import com.biz.primus.base.redis.CrudRedisDao;
import com.biz.primus.base.redis.util.RedisUtil;
import com.biz.primus.base.vo.PageableRequestVo;
import com.biz.primus.ms.member.dao.ro.MemberRo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

/**
 *
 * @author jun.liu
 * @date 2017年10月23日
 * @reviewer
 * @see
 */
@Repository
public class MemberRedisDao extends CrudRedisDao<MemberRo, Long>
{

    private String mobileMappingKey(String mobile) {
        return getKeyByParams("mobile", mobile);
    }

    private String accountMappingKey(String account) {
        return getKeyByParams("account", account);
    }

    private String emailMappingKey(String email) {
        return getKeyByParams("email", email);
    }

    /**
     * 保存
     */
    @Override
    public void save(MemberRo ro) {
        super.save(ro);
        if (StringUtils.isNotBlank(ro.getMobile())) {
            set(this.mobileMappingKey(ro.getMobile()), RedisUtil.toByteArray(ro.getId()));
        }
        if (StringUtils.isNotBlank(ro.getAccount())) {
            set(this.accountMappingKey(ro.getAccount()), RedisUtil.toByteArray(ro.getId()));
        }
        if (StringUtils.isNotBlank(ro.getEmail())) {
            set(this.emailMappingKey(ro.getEmail()), RedisUtil.toByteArray(ro.getId()));
        }
    }

    @Override
    public void delete(MemberRo ro) {
        super.delete(ro);
        if (StringUtils.isNotBlank(ro.getMobile())) {
            del(this.mobileMappingKey(ro.getMobile()));
        }
        if (StringUtils.isNotBlank(ro.getAccount())) {
            del(this.accountMappingKey(ro.getAccount()));
        }
        if (StringUtils.isNotBlank(ro.getEmail())) {
            del(this.emailMappingKey(ro.getEmail()));
        }
    }

    /**
     * 根据mobile查询
     */
    public MemberRo findByMobile(String mobile) {
        if (StringUtils.isBlank(mobile)) {
            return null;
        }else {
            byte[] bs = super.get(this.mobileMappingKey(mobile));
            if (bs == null) {
                return null;
            }
            Long id = RedisUtil.byteArrayToLong(bs);
            return findOne(id);
        }
    }

    /**
     * 根据account查询
     */
    public MemberRo findByAccount(String account) {
        if (StringUtils.isBlank(account)) {
            return null;
        }else {
            byte[] bs = super.get(this.accountMappingKey(account));
            if (bs == null) {
                return null;
            }
            Long id = RedisUtil.byteArrayToLong(bs);
            return findOne(id);
        }
    }

    /**
     * 根据email查询
     */
    public MemberRo findByEmail(String email) {
        if (StringUtils.isBlank(email)) {
            return null;
        }else {
            byte[] bs = super.get(this.emailMappingKey(email));
            if (bs == null) {
                return null;
            }
            Long id = RedisUtil.byteArrayToLong(bs);
            return findOne(id);
        }
    }

    /**
     * 获取会员数量
     * @return
     */
    public Long findMemberCount() {
        return super.zCard(super.getRoSortedSetKey());
    }

    /**
     * 分页获取用户ID
     * @return
     */
    public List<Long> findIdsByPageable(int page, int size) {
        return RedisUtil.bytesSetToLongList(zRange(super.getRoSortedSetKey(), page, size)) == null ? Collections.<Long>emptyList() : RedisUtil.bytesSetToLongList(zRange(super.getRoSortedSetKey(), page, size));
    }

}